package org.example.volunteerend.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.example.volunteerend.entity.ServiceRecord;

@Mapper
public interface ServiceRecordMapper {
    
    /**
     * 添加服务记录
     */
    @Insert("INSERT INTO service_records (user_id, project_id, service_date, service_hours, description, proof_images) " +
            "VALUES (#{record.userId}, #{record.projectId}, #{record.serviceDate}, #{record.serviceHours}, " +
            "#{record.description}, #{proofImagesJson})")
    @Options(useGeneratedKeys = true, keyProperty = "record.recordId")
    int insert(@Param("record") ServiceRecord record, @Param("proofImagesJson") String proofImagesJson);
    
    /**
     * 根据ID查询服务记录详情
     */
    @Select("SELECT sr.*, vp.project_name " +
            "FROM service_records sr " +
            "LEFT JOIN volunteer_projects vp ON sr.project_id = vp.project_id " +
            "WHERE sr.record_id = #{recordId}")
    @Results({
        @Result(property = "recordId", column = "record_id"),
        @Result(property = "userId", column = "user_id"),
        @Result(property = "projectId", column = "project_id"),
        @Result(property = "projectName", column = "project_name"),
        @Result(property = "serviceDate", column = "service_date"),
        @Result(property = "serviceHours", column = "service_hours"),
        @Result(property = "description", column = "description"),
        @Result(property = "proofImages", column = "proof_images", javaType = java.util.List.class)
    })
    ServiceRecord findById(@Param("recordId") Integer recordId);
    
    /**
     * 根据用户ID查询服务记录列表 (使用XML中的SQL定义)
     */
    List<ServiceRecord> findByUserId(@Param("userId") Integer userId);
    
    /**
     * 统计用户服务记录数量 (使用XML中的SQL定义)
     */
    int countByUserId(@Param("userId") Integer userId);
    
    /**
     * 获取服务记录的证明图片 (使用XML中的SQL定义)
     */
    String findProofImagesByRecordId(@Param("recordId") Integer recordId);
} 